//
//  PickerViewController.m
//  Portable Traffic Signal Assistor
//
//  Created by Ding on 9/3/12.
//  Copyright (c) 2012 TTU. All rights reserved.
//

#import "PickerViewController.h"

@interface PickerViewController ()

@end

@implementation PickerViewController
@synthesize pickerView;
@synthesize dataArray=_dataArray;
@synthesize link=_link;
@synthesize agency=_agency;
@synthesize subSystem=_subSystem;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil dataArray:(NSArray *)dataArray link:(Link *)link agency:(Agency *)agency subSystem:(SubSystem *)subsystem
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.dataArray=dataArray;
        self.link=link;
        self.agency=agency;
        self.subSystem=subsystem;
    }
    return self;
}

//-(void) dealloc
//{
//    
//    [super dealloc];
//    
//}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navigationController setToolbarHidden:NO];
    CustomizedBarButtonItem *homeButtonItem=[[[CustomizedBarButtonItem alloc] initCustomizedBarButtonItem:[UIImage imageNamed:@"home.png"] target:self action:@selector(goToHome:)] autorelease];
    self.toolbarItems = [ NSArray arrayWithObjects: homeButtonItem,nil ];
    
    NSArray * data=[self.dataArray objectAtIndex:0];
    int row=0;
    for (int i=0; i<data.count; i++) {
        if ([[[data objectAtIndex:i] intersectionID] isEqualToString:self.link.fromIntersectionID]) {
            row=i;
            break;
        }
    }
    [self.pickerView selectRow:row inComponent:0 animated:NO];
    data=[self.dataArray objectAtIndex:1];
//    int row;
    for (int i=0; i<data.count; i++) {
        if ([[[data objectAtIndex:i] intersectionID] isEqualToString:self.link.toIntersectionID]) {
            row=i;
            break;
        }
    }
    [self.pickerView selectRow:row inComponent:1 animated:NO];
    
    [[self navigationController] setNavigationBarHidden:NO];
    UIBarButtonItem* item = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)] autorelease];
    self.navigationItem.rightBarButtonItem = item;
    
    
//    UINavigationItem *done=[
}

- (void)viewDidUnload
{
    [self setPickerView:nil];
    
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [pickerView release];
    [_dataArray release];
    [_link release];
    [_agency release];
    [_subSystem release];
    [super dealloc];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return [self.dataArray count];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component==0) {
        NSArray * data=[self.dataArray objectAtIndex:component];
        self.link.fromIntersectionID=[[data objectAtIndex:row] intersectionID];
    }
    if (component==1) {
        NSArray * data=[self.dataArray objectAtIndex:component];
        self.link.toIntersectionID=[[data objectAtIndex:row] intersectionID];
    }
    [XMLParser updateLink:self.link agencyID:self.agency.agencyID subSystemID:self.subSystem.subSystemID];
    
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    NSArray * data=[self.dataArray objectAtIndex:component];
    return [data count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    NSArray * data=[self.dataArray objectAtIndex:component];
    return [[data objectAtIndex:row] intersectionName];
}

- (void) goToHome:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void) done:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
